package com.xtgg.login;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.baby.DeviceSearchActivity;
import com.xtgg.main.MainActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.R;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.UmengRegistrar;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SetPasswordActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_password);
		
		if (getIntent().getStringExtra("type").equals("regist")) {
			((TextView) findViewById(R.id.center)).setText("设置密码");
		} else {
			((TextView) findViewById(R.id.center)).setText("重设密码");
		}

		MobclickAgent.onEvent(this, ((TextView) findViewById(R.id.center))
				.getText().toString());

		findViewById(R.id.left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		findViewById(R.id.login).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				getData();
			}
		});
	}

	private void getData() {
		String pwd = ((EditText) findViewById(R.id.pwd)).getText().toString();
		final String pwd0 = ((EditText) findViewById(R.id.pwd0)).getText()
				.toString();

		if (pwd.length() < 8 || pwd0.length() < 8) {
			showToast("密码不能少8位");
			return;
		}

		if (!pwd.equals(pwd0)) {
			showToast("两次密码输入不一致");
			return;
		}

		((Button)findViewById(R.id.login)).setSelected(true);
		((Button)findViewById(R.id.login)).setOnClickListener(null);
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					showToast("密码设置成功");
					sendHttpRequest();
				} else {
					showToast("" + ruser.getMessage());
					
					((Button)findViewById(R.id.login)).setSelected(false);
					((Button)findViewById(R.id.login)).setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							getData();
						}
					});
				}
			}
		};

		RequestParams params = new RequestParams();
		if (getIntent().getStringExtra("type").equals("regist")) {
			params.put("act", "register");
		} else {
			params.put("act", "fogetpwd");
		}
		params.put("mobile", getIntent().getStringExtra("tel"));
		params.put("password", pwd);
		params.put("device_token", UmengRegistrar.getRegistrationId(this));

		MgqRestClient.get(Constants.USERMGT, params, loginHandler);
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this,
				true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					JSONObject obj = JSON.parseObject(response)
							.getJSONArray("data").getJSONObject(0);

					PreferencesUtils.putString(SetPasswordActivity.this, "password",
							((EditText) findViewById(R.id.pwd)).getText().toString());
					PreferencesUtils.putString(SetPasswordActivity.this, "address",
							obj.getString("address"));
					PreferencesUtils.putString(SetPasswordActivity.this, "headurl",
							obj.getString("headurl"));
					PreferencesUtils.putString(SetPasswordActivity.this, "nickname",
							obj.getString("nickname"));
					PreferencesUtils.putString(SetPasswordActivity.this, "telephone",
							obj.getString("telephone"));
					PreferencesUtils.putString(SetPasswordActivity.this, "userid",
							obj.getString("userid"));
					PreferencesUtils.putString(SetPasswordActivity.this, "username",
							obj.getString("username"));
					PreferencesUtils.putString(SetPasswordActivity.this, "sex",
							obj.getString("sex"));

					sendCheckHttpRequest();
				} else {
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "userlogin");
		params.put("mobile", getIntent().getStringExtra("tel"));
		params.put("password", ((EditText) findViewById(R.id.pwd)).getText().toString());
		params.put("device_token", UmengRegistrar.getRegistrationId(this));
		params.put("longitude", ((DemoApp)getApplicationContext()).mLng);
		params.put("latitude", ((DemoApp)getApplicationContext()).mLat);

		MgqRestClient.get(Constants.USERMGT, params, loginHandler);
	}
	
	private void sendCheckHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(SetPasswordActivity.this,
				true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);
				List<Activity> list = ((DemoApp)getApplicationContext()).getActivityList();
				int size = list.size();
				
				if(ruser.getState().equals("1"))
				{
					startActivity(new Intent(SetPasswordActivity.this, MainActivity.class));
				}
				else
				{
					BluetoothAdapter.getDefaultAdapter().enable();
					startActivity(new Intent(SetPasswordActivity.this,DeviceSearchActivity.class));
				}
				
				for(int i = 0; i < size; i++)
				{
					list.get(i).finish();
				}
				finish();
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "getMyDeviceList");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		MgqRestClient.get(Constants.DEVICEMGT, params, loginHandler);
	}
}
